package com.bitdiggers.fa.client.managed.activity;

import com.bitdiggers.fa.client.managed.request.ApplicationRequestFactory;
import com.bitdiggers.fa.client.managed.request.InstitutionProxy;
import com.bitdiggers.fa.client.managed.request.InstitutionRequest;
import com.bitdiggers.fa.client.managed.ui.InstitutionDetailsView;
import com.bitdiggers.fa.client.managed.ui.InstitutionEditView;
import com.bitdiggers.fa.client.managed.ui.InstitutionListView;
import com.bitdiggers.fa.client.managed.ui.InstitutionMobileDetailsView;
import com.bitdiggers.fa.client.managed.ui.InstitutionMobileEditView;
import com.bitdiggers.fa.client.scaffold.ScaffoldApp;
import com.bitdiggers.fa.client.scaffold.place.CreateAndEditProxy;
import com.bitdiggers.fa.client.scaffold.place.FindAndEditProxy;
import com.bitdiggers.fa.client.scaffold.place.ProxyPlace;
import com.google.gwt.activity.shared.Activity;
import com.google.gwt.place.shared.PlaceController;
import com.google.gwt.requestfactory.shared.EntityProxyId;
import com.google.gwt.requestfactory.shared.RequestContext;

public class InstitutionActivitiesMapper {

    private final ApplicationRequestFactory requests;

    private final PlaceController placeController;

    public InstitutionActivitiesMapper(ApplicationRequestFactory requests, PlaceController placeController) {
        this.requests = requests;
        this.placeController = placeController;
    }

    public Activity getActivity(ProxyPlace place) {
        switch(place.getOperation()) {
            case DETAILS:
                return new InstitutionDetailsActivity((EntityProxyId<InstitutionProxy>) place.getProxyId(), requests, placeController, ScaffoldApp.isMobile() ? InstitutionMobileDetailsView.instance() : InstitutionDetailsView.instance());
            case EDIT:
                return makeEditActivity(place);
            case CREATE:
                return makeCreateActivity();
        }
        throw new IllegalArgumentException("Unknown operation " + place.getOperation());
    }

    @SuppressWarnings("unchecked")
    private EntityProxyId<com.bitdiggers.fa.client.managed.request.InstitutionProxy> coerceId(ProxyPlace place) {
        return (EntityProxyId<InstitutionProxy>) place.getProxyId();
    }

    private Activity makeCreateActivity() {
        InstitutionEditView.instance().setCreating(true);
        final InstitutionRequest request = requests.institutionRequest();
        Activity activity = new CreateAndEditProxy<InstitutionProxy>(InstitutionProxy.class, request, ScaffoldApp.isMobile() ? InstitutionMobileEditView.instance() : InstitutionEditView.instance(), placeController) {

            @Override
            protected RequestContext createSaveRequest(InstitutionProxy proxy) {
                request.persist().using(proxy);
                return request;
            }
        };
        return new InstitutionEditActivityWrapper(requests, ScaffoldApp.isMobile() ? InstitutionMobileEditView.instance() : InstitutionEditView.instance(), activity, null);
    }

    private Activity makeEditActivity(ProxyPlace place) {
        InstitutionEditView.instance().setCreating(false);
        EntityProxyId<InstitutionProxy> proxyId = coerceId(place);
        Activity activity = new FindAndEditProxy<InstitutionProxy>(proxyId, requests, ScaffoldApp.isMobile() ? InstitutionMobileEditView.instance() : InstitutionEditView.instance(), placeController) {

            @Override
            protected RequestContext createSaveRequest(InstitutionProxy proxy) {
                InstitutionRequest request = requests.institutionRequest();
                request.persist().using(proxy);
                return request;
            }
        };
        return new InstitutionEditActivityWrapper(requests, ScaffoldApp.isMobile() ? InstitutionMobileEditView.instance() : InstitutionEditView.instance(), activity, proxyId);
    }
}
